﻿/*              Copyright(c) 2008 Dave Sexton              *
 * http://www.codeplex.com/DocProject                      *
 * http://www.codeplex.com/DocProject/Project/License.aspx *
 ***********************************************************/
using System;
using System.Collections.Generic;
using System.Text;

namespace DaveSexton.DocToMaml
{
	/// <summary>
	/// Provides a key for the <see cref="RuleCollection"/> class.
	/// </summary>
	internal struct RuleKey : IEquatable<RuleKey>
	{
		#region Public Properties
		public string Match { get; private set; }

		public RuleMatchKind MatchKind { get; private set; }
		#endregion

		#region Private / Protected
		private readonly bool anyKind;
		#endregion

		#region Constructors
		/// <summary>
		/// Constructs a new instance of the <see cref="RuleKey" /> structure.
		/// </summary>
		public RuleKey(IRule rule)
			: this()
		{
			if (rule == null)
				throw new ArgumentNullException("rule");

			Match = rule.Match;
			MatchKind = rule.MatchKind;
		}

		/// <summary>
		/// Constructs a new instance of the <see cref="RuleKey" /> structure.
		/// </summary>
		private RuleKey(string match, bool anyKind)
			: this()
		{
			Match = match;
			this.anyKind = anyKind;
		}
		#endregion

		#region Methods
		public static RuleKey AnyKind(string match)
		{
			return new RuleKey(match, true);
		}

		public override bool Equals(object obj)
		{
			return obj is RuleKey && Equals((RuleKey) obj);
		}

		public bool Equals(RuleKey other)
		{
			return (anyKind || other.anyKind || MatchKind == other.MatchKind) && string.Equals(Match, other.Match, StringComparison.OrdinalIgnoreCase);
		}

		public override int GetHashCode()
		{
			return (Match ?? "").GetHashCode();
		}

		public override string ToString()
		{
			return MatchKind.ToString() + " " + Match.ToString();
		}
		#endregion

		#region Operator Overloads
		public static bool operator ==(RuleKey v1, RuleKey v2)
		{
			return v1.Equals(v2);
		}

		public static bool operator !=(RuleKey v1, RuleKey v2)
		{
			return !v1.Equals(v2);
		}
		#endregion
	}
}
